package com.bijava.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author bishichu
 * @version 1.0
 * @since 20-11-12
 */
public class CoreUtils {
    public static final char UNDERLINE = '_';
    private CoreUtils() {
        throw new IllegalStateException("CoreUtils class");
    }

    public static String assembleExcelName() {
        return System.currentTimeMillis() + ".xls";
    }

    /**
     * 驼峰格式字符串转换为下划线格式字符串
     *
     * @param param
     * @return
     */
    public static String camelToUnderline(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 下划线格式字符串转换为驼峰格式字符串
     *
     * @param param
     * @return
     */
    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        StringBuilder sb = new StringBuilder(param);
        Matcher mc = Pattern.compile(String.valueOf(UNDERLINE)).matcher(param);
        int i = 0;
        while (mc.find()) {
            int position = mc.end() - (i++);
            sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
        }
        return sb.toString();
    }

    public static Map<String, Object> transformUpperCase(Map<String, Object> objMap) {
        Map<String, Object> resultMap = new HashMap<>();
        if (objMap == null || objMap.isEmpty()) {
            return resultMap;
        }
        Set<String> keySet = objMap.keySet();
        for (String key : keySet) {
            String newKey = key.toUpperCase();
            resultMap.put(newKey, objMap.get(key));
        }
        return resultMap;
    }
}
